package com.mimaxueyuan.disruptor.demo1;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;

/**
 * 使用Producer的方式发布Event
 * 
 * @author: Kevin
 * @官网: 	www.mimaxueyuan.com
 * @Q Q群:	660567408
 * @Email:	mimaxueyuan@163.com
 * [每天进步一点点、人生带来大改变...]
 * [本代码对应视频地址:http://study.163.com/course/introduction/1004573050.htm]
 */
public class LongEventMain1 {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws InterruptedException {
		ExecutorService executor = Executors.newCachedThreadPool();
		LongEventFactory factory = new LongEventFactory();
		int bufferSize = 1024;
		Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>(factory, bufferSize, executor);
		disruptor.handleEventsWith(new LongEventHandler());
		disruptor.start();
		RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
		
		//采用固定模式向RingBuffer中添加事件
		LongEventProducer producer = new LongEventProducer(ringBuffer);
		
		for (long l = 0; true; l++) {
			producer.onData(l);
			Thread.sleep(1000);
		}
	}

}
